package org.kev.transit.worker;

import java.util.ArrayList;
import org.kev.transit.data.Direction;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;
/**
 * Persist a list of data object in the local DB.
 * This is run as a background thread and will not throw any errors
 * @author kevin
 *
 */
public class DirectionUpdateWorker implements Runnable{
	private static final String TAG = "DirectionUpdateWorker";
	private Activity mParent;
	private ArrayList <Direction> mList;
	public DirectionUpdateWorker(Activity parent, ArrayList <Direction> list){
		mParent=parent;
		mList=list;
	}
	public void run(){
		int count = mList.size();
		String where =null;		
		Cursor cursor = null;
		
		for(int i=0;i<count;i++){
			Direction data = mList.get(i);
			//uri.addId(Long.parseLong(s.id) );
			where = Direction.ROUTE_TYPE + "=? AND "+  
					Direction.ROUTE + "=? AND "+
					Direction.DIRECTION_ID+"=? ";					
			//Log.d(TAG,"wheres="+where);
			String args[]={data.routeType,data.route,data.id};
			if(data.routeType==null || data.route==null || data.id==null){ //passing a null here can crash VM
				Log.e(TAG,"Null in parm!"+data.routeType+data.route+data.id);
				return;
			}
			try{
				cursor = mParent.managedQuery(Direction.CONTENT_URI, new String[] 
				     {Direction.DIRECTION_ID},where,args,null);
				//update lookup table
				Log.d(TAG,"found in db #"+cursor.count());
				
				if(cursor.count()==0){
					insertData(data);
				}else{
					updateData(data);
				}
			}catch(Exception e){
				Log.e(TAG,"UNKNOWN error will be ignored",e);
			}finally{
				if(cursor!=null)
					cursor.close();
			}

		}		
		
	}
	/**
	 * insert data object into DB table
	 * @param data
	 * @return row URI
	 */
	private Uri insertData(Direction data){
		//Log.d(TAG,">>> insert Direction"+mParent+data);
		if (mParent ==null || data==null ){
			return null;
		}

		Log.d(TAG, "+++insertDirection");
		Log.d(TAG, data.toXml() );
		ContentValues values = new ContentValues();
		values.put(Direction.DIRECTION_ID, data.id);
		values.put(Direction.TITLE, data.title);
		values.put(Direction.ROUTE_TYPE, data.routeType);
		values.put(Direction.ROUTE, data.route);		

		values.put(Direction.CREATE_DATE,new  Long(System.currentTimeMillis() ) );
		Uri curl = mParent.getContentResolver().insert(Direction.CONTENT_URI,values);
		//Log.d(TAG,"<<< insertDirection"+curl);
		return curl;
	}

	/**
	 * update date in DB with data object
	 * @param data
	 * @return row count
	 */
	private int updateData(Direction data){
		if (mParent ==null|| data==null){
			return 0;
		}
		Log.d(TAG, "+++updateDirection");
		Log.d(TAG, data.toXml() );
		ContentValues values = new ContentValues();
		values.put(Direction.DIRECTION_ID, data.id);
		values.put(Direction.ROUTE_TYPE, data.routeType);
		values.put(Direction.ROUTE, data.route);		
		values.put(Direction.TITLE, data.title);

		values.put(Direction.CREATE_DATE,new  Long(System.currentTimeMillis() ) );
		String where = Direction.DIRECTION_ID+"=? AND "+data.routeType+"=? AND "+
						Direction.ROUTE+"=? AND "+data.route+"=?";
		String[] args={data.id,data.routeType,data.route};

		int row = mParent.getContentResolver().update(Direction.CONTENT_URI,values,where,args);
		Log.d(TAG,"row="+row );
		return row;
	}
}
